package org.client.iot.ui.project;

import static org.client.iot.constant.GlobeConstant.PAGE_DEFINE.PAGE_ONE;
import static org.client.iot.constant.GlobeConstant.PAGE_DEFINE.PAGE_TWO;

import android.widget.RadioGroup;

import androidx.viewpager2.widget.ViewPager2;

import org.client.iot.R;
import org.client.iot.databinding.ActivityProjectHomeBinding;
import org.client.iot.ui.device.adapter.BottomMenuAdapter;
import org.client.iot.ui.project.fragment.ProjectHomeFragment;
import org.client.iot.ui.project.fragment.ProjectMyselfFragment;
import org.tzw.template.base.BaseActivity;

import java.util.ArrayList;
import java.util.Arrays;

public class ProjectHomeActivity extends BaseActivity<ActivityProjectHomeBinding> implements RadioGroup.OnCheckedChangeListener {
    private BottomMenuAdapter mAdapter;

    @Override
    protected void initData() {
        mAdapter = new BottomMenuAdapter(getSupportFragmentManager(), getLifecycle()
                , new ArrayList<>(Arrays.asList(new ProjectHomeFragment(), new ProjectMyselfFragment())));
    }

    @Override
    protected void initView() {
        mRoot.viewPagerProjectFragment.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        mRoot.viewPagerProjectFragment.setAdapter(mAdapter);
        mRoot.viewPagerProjectFragment.setCurrentItem(0);
        mRoot.viewPagerProjectFragment.setOffscreenPageLimit(2);

        mRoot.rbMenuProjectHome.setChecked(true);
    }

    @Override
    protected void initListener() {
        mRoot.rgBottomMenu.setOnCheckedChangeListener(this);
        mRoot.viewPagerProjectFragment.registerOnPageChangeCallback(onPageChangeCallback);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_menu_project_home) {
            mRoot.viewPagerProjectFragment.setCurrentItem(PAGE_ONE);
        } else if (checkedId == R.id.rb_menu_project_myself) {
            mRoot.viewPagerProjectFragment.setCurrentItem(PAGE_TWO);
        }
    }

    private ViewPager2.OnPageChangeCallback onPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            switch (position) {
                case PAGE_ONE:
                    mRoot.rbMenuProjectHome.setChecked(true);
                    break;
                case PAGE_TWO:
                    mRoot.rbMenuProjectMyself.setChecked(true);
                    break;
            }
        }
    };
}